package com.woniuxy.moneyapp.app.jobhandler;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.woniuxy.commons.exception.MyException;
import com.woniuxy.commons.util.Result;
import com.woniuxy.moneyapp.common.exception.code.FundExceptionCode;
import com.woniuxy.moneyapp.infra.dto.RefundInfo;
import com.woniuxy.userapp.client.VenueClient;
import com.woniuxy.userapp.client.param.VenueOpenOrCloseInfo;
import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.*;

/**
 * XxlJob开发示例（Bean模式）
 *
 * 开发步骤：
 *      1、任务开发：在Spring Bean实例中，开发Job方法；
 *      2、注解配置：为Job方法添加注解 "@XxlJob(value="自定义jobhandler名称", init = "JobHandler初始化方法", destroy = "JobHandler销毁方法")"，注解value值对应的是调度中心新建任务的JobHandler属性的值。
 *      3、执行日志：需要通过 "XxlJobHelper.log" 打印执行日志；
 *      4、任务结果：默认任务结果为 "成功" 状态，不需要主动设置；如有诉求，比如设置任务结果为失败，可以通过 "XxlJobHelper.handleFail/handleSuccess" 自主设置任务结果；
 *
 * @author xuxueli 2019-12-11 21:52:51
 */
@Component
@Slf4j
public class DelayJob {

    private static Logger logger = LoggerFactory.getLogger(DelayJob.class);


    @Resource
    private StringRedisTemplate srt;

    @Resource
    private VenueClient vc;

    @XxlJob("closeVenueByOrderState")
    public void closeVenueByOrderState() throws Exception {
        System.out.println("执行器执行===");
        String jobParam = XxlJobHelper.getJobParam();
        RefundInfo refundInfo = new ObjectMapper().readValue(jobParam, RefundInfo.class);
        VenueOpenOrCloseInfo venueOpenOrCloseInfo = new VenueOpenOrCloseInfo();
        venueOpenOrCloseInfo.setVenueId(refundInfo.getVenueId());
        venueOpenOrCloseInfo.setOpenOrCloseCode(2);
        Result result = vc.venueOpenOrClose(venueOpenOrCloseInfo);
        System.out.println(result);
        if(result.getCode()!=200)
            throw new MyException(FundExceptionCode.VENUE_STATE_CHANGE_ERROE.getCode(),FundExceptionCode.VENUE_STATE_CHANGE_ERROE.getMsg());
    }

    @XxlJob("test")
    public void test() throws Exception {
        System.out.println("执行器执行===");
    }
}
